const db = require("../../db")

// 问题信息的发布（用户的connect_num+1)
exports.addQue = (req,res)=>{
    const queInfo = {
        // title,content
        ...req.body,
        author:req.user.username,
        aut_id:req.user.id,
        pubdata:new Date(),
        view:0,
        commentNum:0,
        isdelete:0,
        likes:0,
        isdown:0
    }
    var str = "insert into question set ?"
    db.query(str,queInfo,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows != 1) return res.cc("数据库错误,请重新发布")
        // 获取用户的connect_num
        str = "select  connect_num from user where id = ?"
        db.query(str,req.user.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.length != 1)  return res.cc("数据库错误,请重新发布")
            var dataString = JSON.stringify(results);
            results = JSON.parse(dataString);
            var connect_num = results[0].connect_num +1
            // 更新用户的connect_num
            str = "update user set connect_num = ? where id =?"
            db.query(str,[connect_num,req.user.id],(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("数据库错误,用户发布量增添失败")
                res.cc("发布成功",0)
            })
        })
    })
}

// 问题信息的删除（用户的connect_num-1）用户只能删除自己发布的  假删除

exports.delQue = (req,res)=>{
    // 根据传入的文章id号查找文章
    var str = "select * from question where id = ?"
  
    db.query(str,req.body.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误,请重新删除")
      
        if(req.user.id == results[0].aut_id){
            str = "update  question set isdelete = 1 where id = ?"
            db.query(str,req.body.id,(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("数据库错误,请重新删除")
                // 获取用户的connect_num
                str = "select  connect_num from user where id = ?"
                db.query(str,req.user.id,(err,results)=>{
                    if(err) return res.cc(err)
                    if(results.length != 1)  return res.cc("数据库错误,请重新发布")
                    var dataString = JSON.stringify(results);
                    results = JSON.parse(dataString);
                    var connect_num = results[0].connect_num
                    if(connect_num <= 0){
                        connect_num = 0
                    }else{
                        connect_num -= 1
                    }
                    // 更新用户的connect_num
                    str = "update user set connect_num = ? where id =?"
                    db.query(str,[connect_num,req.user.id],(err,results)=>{
                        if(err) return res.cc(err)
                        if(results.affectedRows != 1) return res.cc("数据库错误,用户发布量增添失败")
                        res.cc("删除成功",0)
                    })
                })
              
            })
        }else{
            res.cc("该问答交流文章不是您发布的哦")
        }
    })
}

// 问题信息的删除   用户只能删除自己发布的  真正删除，并删除文章的相关评论

exports.rDelQue = (req,res)=>{
    // 根据传入的文章id号查找文章
    var str = "select * from question where id = ?"
   
    db.query(str,req.body.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误1,请重新删除")
      
        if(req.user.id == results[0].aut_id){
            str = "delete from question where id = ?"
            db.query(str,req.body.id,(err,results)=>{

                if(err) return res.cc(err)
              
                // res.cc("删除成功",0)    
                str = "delete from comments where t_id = ?"
                db.query(str,req.body.id,(err,results)=>{
                    if(err) return res.cc(err)
                  
                    res.cc("删除成功",0) 
                })
            })
        }else{
            res.cc("该问答交流文章不是您发布的哦")
        }
    })
}

// 问题信息的删除找回   用户只能找回自己发布的

exports.backDelQue = (req,res)=>{
    // 根据传入的文章id号查找文章
    var str = "select * from question where id = ?"
   
    db.query(str,req.body.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误,请重新查询")
      
        if(req.user.id == results[0].aut_id){
            str = "update question set isdelete = 0 where id = ?"
            db.query(str,req.body.id,(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("数据库错误,请重新查询")
                // 获取用户的connect_num
                str = "select  connect_num from user where id = ?"
                db.query(str,req.user.id,(err,results)=>{
                    if(err) return res.cc(err)
                    if(results.length != 1)  return res.cc("数据库错误,请重新发布")
                    var dataString = JSON.stringify(results);
                    results = JSON.parse(dataString);
                    var connect_num = results[0].connect_num
                    
                        connect_num += 1
                   
                    // 更新用户的connect_num
                    str = "update user set connect_num = ? where id =?"
                    db.query(str,[connect_num,req.user.id],(err,results)=>{
                        if(err) return res.cc(err)
                        if(results.affectedRows != 1) return res.cc("数据库错误,用户发布量增添失败")
                        res.cc("找回成功",0)
                    })
                })
               
              
            })
        }else{
            res.cc("该问答交流文章不是您发布的哦")
        }
    })
}


// 问题信息的下架   用户只能下架自己发布的

exports.downQue = (req,res)=>{
    // 根据传入的文章id号查找文章
    var str = "select * from question where id = ?"
   
    db.query(str,req.body.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误,请重新下架")
      
        if(req.user.id == results[0].aut_id){
            str = "update  question set isdown = 1 where id = ?"
            db.query(str,req.body.id,(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("数据库错误,请重新下架")
                res.cc("下架成功",0)
              
            })
        }else{
            res.cc("该问答交流文章不是您发布的哦")
        }
    })
}

// 问题信息的下架找回   用户只能找回自己发布的

exports.backDownQue = (req,res)=>{
    // 根据传入的文章id号查找文章
    var str = "select * from question where id = ?"
 
    db.query(str,req.body.id,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length != 1) return res.cc("数据库错误,请重新查询")
      
        if(req.user.id == results[0].aut_id){
            str = "update question set isdown = 0 where id = ?"
            db.query(str,req.body.id,(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows != 1) return res.cc("数据库错误,请重新查询")
                res.cc("恢复成功",0)
              
            })
        }else{
            res.cc("该问答交流文章不是您发布的哦")
        }
    })
}

// 交流问答的点赞
exports.likeQue = (req,res)=>{
    var id  =  parseInt(req.body.id)
   
    // 获取点赞量且+1
    var str = "select * from question where id = ?"
    db.query(str,id,(err,results)=>{
        if(err) res.cc(err)
        var likes = results[0].likes + 1
       
        str = "update question set likes = ? where id = ?"
        db.query(str,[likes,id],(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("数据库错误,请重新点赞")
            res.cc("点赞成功",0)
        })
    })
}
// 交流问答取消点赞
exports.unLikeQue = (req,res)=>{
    var id  =  parseInt(req.body.id)
   
    // 获取点赞量且+1
    var str = "select * from question where id = ?"
    db.query(str,id,(err,results)=>{
        if(err) res.cc(err)
        var likes = results[0].likes
        if(likes <=0){
            likes = 0
        }else{
            likes -=1
        }
       
        str = "update question set likes = ? where id = ?"
        db.query(str,[likes,id],(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("数据库错误,请重新取消点赞")
            res.cc("取消点赞成功",0)
        })
    })
}


// 查询二手商品的信息详情
// exports.getDetailQuestion = (req,res)=>{
//     console.log(req.query)
//     var str = "select q.* from question q join user u on  q.id = ? where u.id = ? "
//     db.query(str,[req.query.id,req.user.id],(err,results)=>{
//         if(err) return res.cc(err)
//         if(results.length != 1) return res.cc("查询失败，稍后请重新尝试")
//         console.log(results)
//         res.send({
//             status:0,
//             message:"查询成功",
//             data:results
//         })
//     })
// }

exports.getDetailQuestion = (req,res)=>{
    const id = req.query.id
    var str = "select * from question where id = ?"
   
        db.query(str,id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.length != 1) return res.cc("数据库错误，稍后请重新尝试")
           
            var views = results[0].view+1
            str = "update question set view = ? where id =?"
            db.query(str,[views,id],(err,result)=>{
                if(err) return res.cc(err)
                if(result.affectedRows != 1) return res.cc("访客记录获取失败")
                res.send({
                    status:0,
                    data:results[0]
                })
            })
        })
}